using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;

namespace WindowsApplication4
{
    public class Player 
    {
        DataRow drPlayer;

        public Player(DataRow player)
        {
            this.drPlayer = player;

        }

        public int Points
        {
            get
            {
                return Convert.ToInt32(drPlayer[World.CONSTS.PlayersTable.Columns.PointsIndex]);
            }
        }

        public int ID
        {
            get
            {
                return Convert.ToInt32(drPlayer[World.CONSTS.PlayersTable.Columns.IDIndex]);
            }
        }


        public string Name
        {
            get
            {
                return drPlayer[World.CONSTS.PlayersTable.Columns.NameIndex].ToString();
            }
        }

        public Tribe Tribe
        {
            get
            {
                DataRow drTribe = drPlayer.GetParentRow(Worlds.currentWorld.dtPlayers.ParentRelations[0]);
                if (drTribe == null)
                {
                    return null;
                }
                else
                {
                    Tribe t = new Tribe(drTribe);

                    if (t.ID == 0) //player is tribless
                    {
                        return null;
                    }
                    else
                    {
                        return t;
                    }
                }

            }
        }


        private class VillageRowSorter : System.Collections.IComparer
        {

            // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
            int System.Collections.IComparer.Compare(Object x, Object y)
            {
                DataRow rowX = (DataRow)x;
                DataRow rowY = (DataRow)y;
                return ((new CaseInsensitiveComparer()).Compare((string)rowX[World.CONSTS.VillagesTable.Columns.NameIndex]
                    ,(string)rowY[World.CONSTS.VillagesTable.Columns.NameIndex]));
                    
            }

        }


        /// <summary>
        /// villages sorted by name
        /// </summary>
        public System.Collections.Generic.List<Village> Villages
        {
            get
            {
                DataRow[] rows = drPlayer.GetChildRows(World.CONSTS.Relations.PlayersToVillages);
                if (rows.Length == 0)
                {
                    return new List<Village>(0);
                }
                else
                {
                    List<Village> villages = new List<Village>(rows.Length);
                    Array.Sort(rows, new VillageRowSorter());

                    foreach (DataRow dr in rows)
                    {
                        villages.Add(new Village(dr));
                    }

                    return villages;
                }
            }
        }

        public  void CheckForLatestIntel()
        {
            ReportRetrieve.ReportRetrieve retrieve = new WindowsApplication4.ReportRetrieve.ReportRetrieve();
            DataSet ds = retrieve.RetrieveForPlayer(this.Name, new Guid("1B0FBEF2-1DF4-4183-BE57-F58BCA7BF154"));

            if (ds.Tables.Count != 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Village vil = Worlds.currentWorld.Village((int)dr["VillageXCord"], (int)dr["VillageYCord"]);
                    vil.SaveLatestIntel(dr);
                }
            }
        }
    }
}
